package com.auto.learning.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.adapter.SleepModeTimeAdapter;
import com.auto.learning.bean.SleepModelBean;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.DateUtil;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;
import com.auto.learning.view.SwitchButton;

/**
 * 定时关闭
 */
public class MySleepModeActivity extends BaseActivity implements OnCheckedChangeListener {
	
	private GridView gv_sleepmode;
	private SleepModeTimeAdapter adapter;
	private ArrayList<SleepModelBean> list = new ArrayList<SleepModelBean>(); 
	private SwitchButton button_colse_play;

	private LinearLayout linear_top;
	private TextView tv_count_time;
	CountdownRunnable runnable;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setViewInLine(R.layout.sleepmode_activity);
		initData();
		initView();
		TemporaryVariable.activities.push(this);
	}
	
	@Override
	protected void onDestroy() {
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
	
	private void initView(){
		setTitle(R.string.favor_09);
		hideRight();
		runnable = new CountdownRunnable();
		linear_top = (LinearLayout) findViewById(R.id.linear_top1);
		button_colse_play = (SwitchButton) findViewById(R.id.button_colse_play);
		button_colse_play.setChecked(!(boolean)SharedPreferencesUtils.getParam(this, HttpRequestHelper.M_TILE_OFF, false));
		button_colse_play.setOnCheckedChangeListener(this);
		if ((boolean)SharedPreferencesUtils.getParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_OFF, false)) {
			linear_top.setVisibility(View.VISIBLE);
		} else {
			linear_top.setVisibility(View.GONE);
		}
		handler.post(runnable);
		tv_count_time = (TextView) findViewById(R.id.tv_count_time);
		gv_sleepmode = (GridView) findViewById(R.id.gv_sleepmode);
		adapter = new SleepModeTimeAdapter(this, list);
		adapter.setSelectedPosition((int)SharedPreferencesUtils.getParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_POSITION, 2));
		gv_sleepmode.setAdapter(adapter);
		gv_sleepmode.setSelector(new ColorDrawable(Color.TRANSPARENT));
		
		gv_sleepmode.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				int closeMins = 0; 
				switch (list.get(position).getType()) {
				case 1:
					closeMins = Integer.parseInt(list.get(position).getTime());
					break;
				case 2:
					if (list.get(position).getTime().equals("1.5")) {
						closeMins = 90;
					}else if (list.get(position).getTime().equals("2.5")) {
						closeMins = 150;
					} else {
						closeMins = Integer.parseInt(list.get(position).getTime()) * 60;
					}
					break;
				}
				SharedPreferencesUtils.setParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_VALUE, closeMins);
				SharedPreferencesUtils.setParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_POSITION, position);
				adapter.setSelectedPosition(position);
				adapter.notifyDataSetChanged();
				
				if ((boolean)SharedPreferencesUtils.getParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_OFF, false)) {
					CommonMethod.cancelAlarm(MySleepModeActivity.this);
					CommonMethod.setAlarm(MySleepModeActivity.this);
					linear_top.setVisibility(View.VISIBLE);
				} else {
					CommonMethod.cancelAlarm(MySleepModeActivity.this);
					linear_top.setVisibility(View.GONE);
				}
			}
		});
	}
	
	/**
	 * 初始化数据
	 */
	private void initData(){
		list.add(new SleepModelBean("10", 1));
		list.add(new SleepModelBean("20", 1));
		list.add(new SleepModelBean("30", 1));
		list.add(new SleepModelBean("45", 1));
		list.add(new SleepModelBean("1", 2));
		list.add(new SleepModelBean("1.5", 2));
		list.add(new SleepModelBean("2", 2));
		list.add(new SleepModelBean("2.5", 2));
		list.add(new SleepModelBean("3", 2));
		list.add(new SleepModelBean("4", 2));
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.button_colse_play:
			SharedPreferencesUtils.setParam(this, HttpRequestHelper.M_TILE_OFF, !isChecked);
			if (!isChecked) {
				CommonMethod.cancelAlarm(MySleepModeActivity.this);
				CommonMethod.setAlarm(MySleepModeActivity.this);
				linear_top.setVisibility(View.VISIBLE);
			}else {
				CommonMethod.cancelAlarm(MySleepModeActivity.this);
				linear_top.setVisibility(View.GONE);
			}
			break;
		}
	}
	
	/**
	 * 倒计时
	 *
	 */
	class CountdownRunnable implements Runnable{

		@Override
		public void run() {
		    String alarmTime = (String) SharedPreferencesUtils.getParam(MySleepModeActivity.this, HttpRequestHelper.M_TILE_WNEN_CLOSE, "");
			if (!TextUtils.isEmpty(alarmTime)) {
				SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long count;
				try {
					count = sdf.parse(alarmTime).getTime() - System.currentTimeMillis();
					if (count > 0) {
						tv_count_time.setText(Html.fromHtml(DateUtil.formatDuring3(count) + " 后停止播放"));
					}else {
						linear_top.setVisibility(View.GONE);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			handler.postDelayed(this, 1000);
		}
	};
}
